
@extends('layouts.default')

@section('title', '')

@push('scripts')
<script>
    layui.use(['layer', 'element','form','table','laypage','laytpl','laydate','upload'], function(){
        var layer = layui.layer;
        var form = layui.form;
        var element = layui.element;
        var table = layui.table;
        var laypage = layui.laypage;
        var laytpl = layui.laytpl;
        var laydate = layui.laydate;
        var upload = layui.upload;
        var param = {};


        getList(1);
        renderSelect();

        //日期时间范围
        laydate.render({
            elem: '#test10'
            ,type: 'date'
            ,range: true
        });

        //下拉框初始化
        function renderSelect(){
            getDictionaryItem({'diccode': ['award_type','level','award_grade','GRADE', 'CLASS'],'from_type':1}, function (res){
                if(res.code === 1) {
                    grade = res.data.GRADE;
                    classItem = res.data.classItem;
                    console.log(grade)
                    let data = res.data;
                    for(var k in data) {
                        let key = k.toLowerCase();
                        if(k == 'hdjb'){
                            $('[data-role='+key+'List]').empty().append('<option value="">请选择</option>');
                        }else{
                            $('[data-role='+key+'List]').empty().append('<option value="">请选择</option>');
                        }
                        let d = data[k];
                        for (var i in d) {
                            $('[data-role='+key+'List]').append('<option value="' + d[i].optionId + '">' + d[i].selectValue + '</option>');
                        }
                    }
                    form.render();
                }
            });
            getTermData({},function(res){
                if(res.code == 1){
                    $('select[name=data_version_id]').empty();
                    $('select[name=data_version_id]').append('<option value="">请选择</option>');
                    for(var i in res.data){
                        $('select[name=data_version_id]').append('<option value="'+res.data[i].id+'">'+res.data[i].title+'</option>');
                    }
                    form.render();
                }
            })
        }

        // 查询
        form.on('submit(queryBtn)', function(data) {
            Object.assign(param, {
                    "search": data.field
            });
            getList(1);
            return false;
        });

        // 导出
        form.on('submit(exportBtn)', function(data) {
            Object.assign(param, {
                    "search": data.field
            });
            exportHonor(param,function(res){
                if(res.code == '1'){
                    exportsDel('班级荣誉.xlsx','/download/excel/'+res.data.file_name);
                }else{
                    layer.msg(res.msg, {icon: 5,shift : 6});
                }
            });
            return false;
        });

        //导出excel
        function exportsDel(showName,fileName){
            window.location.href = "{{route('download.FileDownloadDel')}}"+'?filename='+showName+'&file_url='+fileName;
        }

        //重置
        $('button[type=reset]').click(function(){
            $('#search-form')[0].reset();
            $('button[lay-filter=queryBtn]').click();
            return false;
        })

        // 导入
        form.on('submit(importBtn)', function(data) {
            layer.open({
                title:'数据上传',
                type: 1,
                area: ['620px', '360px'],
                content: $('#import-record'), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                btn: [ '确认', '取消'],
                btnAlign: 'c',
                yes: function(index, layero){
                    //按钮【按钮一】的回调
                    layer.closeAll();
                },
                btn2: function(index, layero){
                    //按钮【按钮二】的回调
                    layer.closeAll();
                    //return false 开启该代码可禁止点击该按钮关闭
                },
                success: function(layero, index){
                    // $('#import-record').find("input[name='term_id']").val(TermId);
                },
                end: function() {
                    getList(1);
                }
            });
            return false;
        });



        function getList(page){
            param.honor = 1;
            Object.assign(param,{"page":page});
            getAwardsList(param,function(res){
                if(res.code == 1){
                    table.render({
                        elem: '#tablelist'
                        ,toolbar: '#toolbarDemo'
                        ,defaultToolbar:[]
                        ,data: res.data.data
                        ,limit:res.data.limit
                        ,skin:'nob'
                        ,even:true
                        //,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                        ,cols: [[
                        {field: '', title: '学年学期',width:'10%',align:'center',templet: function(d) {
                            // return (page-1)*res.data.limit+d.LAY_INDEX;
                            if(d.data_version_name === null){
                                return '';
                            }else{
                                return d.data_version_name;
                            }
                        }}
                        ,{field: 'name', title: '奖项名称',width:'10%',align:'center'}
                        ,{field: 'level', title: '年级班级', align: 'center', templet: function(d) {
                                    grade = d.grade_array ? d.grade_array[0].selectValue : '-';
                                    classValue = d.class_item_set ? d.class_item_set[0].selectValue : '';
                                    return grade+classValue;
                        }}
                        ,{field: '', title: '级别',width:'5%',align:'center',templet: function(d) {
                                if(d.level_array.length>0){
                                    return d.level_array[0].selectValue;
                                }else{
                                    return '';
                                }
                        }}
                        ,{field: '', title: '状态',width:'10%',align:'center',templet: function(d) {
                                if(d.status == 'FILL_IN'){
                                    return '未提交';
                                }else if(d.status == 'REVIEW'){
                                    return '待审核';
                                }else if(d.status == 'REVIEW_FAIL'){
                                    return '已驳回';
                                }else if(d.status == 'PASS'){
                                    return '审核通过';
                                }else{
                                    return '-';
                                }

                        }}

                       ,{field: 'nickname', title: '创建人',width:'10%',align:'center'}
                        ,{field: 'created_at', title: '创建日期', align: 'center'}
                        ,{field: '', title: '操作',toolbar: '#barDemo',width:'',align:'center'}
                        ]]
                    });

                    $('#pageBar').empty();
                    if(res.data.total >0){
                        //分页
                        laypage.render({
                        elem: 'pageBar' //注意，这里的 test1 是 ID，不用加 # 号
                        ,count: [res.data.total] //数据总数，从服务端得到
                        ,curr: page
                        ,layout: ['limit', 'count', 'prev', 'page', 'next',  'skip']
                        ,limit:res.data.limit
                        ,jump: function(obj, first) {
                                //首次不执行
                                if (!first) {
                                    Object.assign(param,{"limit":obj.limit});
                                    getList(obj.curr)
                                }
                            }
                        });
                    }
                     setBtnPermission();
                }else {
                    layer.msg(res.msg, {icon: 5});
                }

            })
        }

        laydate.render({
            elem: '#range-date'
            ,type: 'date'
            ,range: true
        });

        table.on('tool(tablelist)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

            if(layEvent === 'del'){ //删除
                layer.confirm('确定删除吗?', function(index){
                    delAwards({'id':data.id},function(res){
                        if(res.code == '1'){
                            layer.msg(res.msg,{icon: 6,time: 1000});
                            obj.del();
                            layer.close(index);
                        }else{
                            layer.msg(res.msg, {icon: 5,shift : 6});
                        }
                    })


                });
            }else if(layEvent === 'edit'){
              window.location.href = "{{route('honor.honor.add')}}?id="+data.id;

            }else if(layEvent === 'check'){
                window.location.href = "{{route('honor.honor.check')}}?id="+data.id;

            }else if(layEvent === 'see'){
                window.location.href = "{{route('honor.honor.see')}}?id="+data.id;
            }else if(obj.event === 'push'){
                layer.confirm('确定此处理吗?', function(index){
                    dealPush({'id':data.id},function(res){
                        if(res.code == '1'){
                            var tr = obj.tr;
                            layer.msg(res.msg,{icon: 6,time: 1000});
                            //alert(res.data != 1?'推送':'取消推送')
                            $(tr).find('.push').text(res.data != 1?'推送':'取消推送')
                            layer.close(index);
                        }else{
                            layer.msg(res.msg, {icon: 5,shift : 6});
                        }
                    })


                });
            }
        });


        //上传excel文件
        upload.render({
            elem: '#btn-upload'
            ,url: "{{route('honor.honor.importHonor')}}" //改成您自己的上传接口
            ,accept: 'file'
            ,size: '20480'
            ,exts: 'xlsx|xls'
            ,headers: {"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr('content')}
            ,done: function(res){
                layer.closeAll('loading');
                if(res.code == 1){
                    $('#import-record').find('.msg-span').text(res.data.msg);
                    if(res.data.is_fail =='1'){
                        layer.msg(res.msg,{icon: 6,time: 2000});
                    }else{
                        $('#import-record').find('.but-1').hide();
                        $('#import-record').find('.but-2').show().attr('file-name',res.data.file_name);
                    }
                }else{
                    $('#import-record').find('.msg-span').text('');
                    layer.msg(res.msg, {icon: 5,shift : 6});
                }
            }
            ,before: function(obj){
                //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                // let termId = $('#term-id').val();

                // this.data = {
                //     term_id: termId
                // }

                $('#import-record').find('.msg-span').text('正在导入...');
                layer.load(); //上传loading
            }
            ,error: function(index, upload){
                layer.closeAll('loading');
            }
        });

        //下载错误文件
        $('#import-record').find('.but-2').click(function(){
            window.location.href = "{{route('download.FileDownload')}}"+'?filename=任教学科数据导入错误信息.xlsx&file_url=' + $(this).attr('file-name');
        });
    })
</script>


<script type="text/html" id="barDemo">


    <a class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="see"  btnPermission="385">查看</a>
    @{{# if(d.is_part == 0 ){ }}
    @{{# if(d.status == 'REVIEW'){ }}
    <a class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="check"  btnPermission="386">审核</a>
    @{{# }else{ }}
    <a class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs layui-btn-disabled" lay-event=""  btnPermission="386">审核</a>
    @{{# } }}


    @if(auth()->user()->cur_role_id == 2)
        @{{# if(d.status == 'FILL_IN' || d.status == 'REVIEW_FAIL'){ }}
        <a class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs"  lay-event="edit" btnPermission="383">编辑</a>
        @{{# }else{ }}
        <a class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs layui-btn-disabled" lay-event=""  btnPermission="383">编辑</a>
        @{{# } }}
    @else
        <a class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="edit"  btnPermission="383">编辑</a>
    @endif

    @if(auth()->user()->cur_role_id == 2)
        @{{# if(d.status != 'FILL_IN' && d.status != 'REVIEW_FAIL'){ }}
        <a class="layui-btn layui-btn-primary layui-btn-xs layui-btn-disabled" lay-event="" btnPermission="384">删除</a>
        @{{# }else{ }}
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="del" btnPermission="384">删除</a>
        @{{# } }}
    @else
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="del" btnPermission="384">删除</a>
    @endif
    <a btnPermission="394" class="layui-btn push layui-btn-primary layui-btn-xs @{{# if(d.status == 'PASS'){ }} layui-border-blue@{{# }else{ }}layui-btn-disabled @{{# } }}"  @{{# if(d.status=='PASS'){ }}lay-event="push"@{{# } }}  >
        @{{# if(d.is_push == '1'){ }}取消推优@{{# }else{}}推优@{{# } }}</a>
    @{{# } }}
</script>
<!---头部工具栏--->
<script type="text/html" id="toolbarDemo">

    <div class="list-head">
            <h3>班级荣誉列表</h3>
            <div class="layui-btn-container list-opt-btns">
                <a btnPermission="382" class="layui-btn layui-btn-normal" href="{{route('honor.honor.add')}}" id="addBtn"> + 新增 </a>
            </div>
        </div>
</script>

@endpush

@section('content')
    <style>
        #search-form .layui-input-inline{
            width:140px !important;
        }
        #search-form .layui-form-label{
            width: 70px;
        }
        #search-form .layui-form-label{
            padding: 9px 5px;
        }
    </style>
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-row">
                <form class="layui-form" id="search-form" >
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label" >奖项名称:</label>
                            <div class="layui-input-inline">
                                <input autocomplete="off" type="text" name="name" class="layui-input" placeholder="请输入">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label" >学年学期:</label>
                            <div class="layui-input-inline">
                                <select name="data_version_id" class="layui-select" lay-filter="term_id" data-role="term_id" style="width:190px;"></select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">年级：</label>
                            <div class="layui-input-inline">
                                <select name="grade" lay-filter="gradeList" data-role="gradeList"></select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">班级：</label>
                            <div class="layui-input-inline">
                                <select name="class_item" lay-filter="classList" data-role="classList"></select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label" >级别:</label>
                            <div class="layui-input-inline">
                                <select data-role="levelList" name="level"   lay-filter="" ></select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">时间范围：</label>
                            <div class="layui-input-inline">
                                <input type="text" name="date_range" id="range-date" placeholder="请选择" autocomplete="off" class="layui-input">
                            </div>
                        </div>


                        <div class="layui-inline">
                            <label class="layui-form-label" >创建人:</label>
                            <div class="layui-input-inline">
                                <input autocomplete="off" type="text" name="nickname" class="layui-input" placeholder="请输入">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label" >状态:</label>
                            <div class="layui-input-inline">
                                <select data-role="" name="status"   lay-filter="" >
                                    <option value="">请选择</option>
                                    <option value="FILL_IN">未提交</option>
                                    <option value="REVIEW">待审核</option>
                                    <option value="REVIEW_FAIL">已驳回</option>
                                    <option value="PASS">审核通过</option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline" >
                                <button lay-submit id="queryBtn" class="pear-btn pear-btn-md pear-btn-primary" lay-filter="queryBtn" >
                                    查询
                                </button>
                                <button type="reset" class="pear-btn pear-btn-md reset">
                                    重置
                                </button>
                                <button lay-submit id="exportBtn" class="pear-btn pear-btn-md pear-btn-primary" lay-filter="exportBtn" btnPermission="392">
                                    导出
                                </button>
                                <button lay-submit id="importBtn" class="pear-btn pear-btn-md pear-btn-primary" lay-filter="importBtn" btnPermission="399">
                                    导入
                                </button>
                                <a btnPermission="331" class="pear-btn pear-btn-md pear-btn-primary" href="{{ route('template.excelDownload') }}?file_name=honor_template.xlsx&show_name=班级荣誉上传模板.xlsx" id="downloadBtn" lay-event="templateDownload" btnPermission="399" >
                                    导入模板下载
                                </a>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>


    <div class="layui-card">

        <div class="layui-card-body">
            <table class="layui-table" id="tablelist" lay-filter="tablelist" ></table>
            <div class="pagebarbox" style="text-align: end"><div id="pageBar" class="pagebar"></div></div>
        </div>
    </div>
    <!-- 文件导入 -->
    <div id="import-record" style="display:none; padding:30px 20px;">
        <div class="layui-row">
            <button class="pear-btn pear-btn-primary pear-btn-sm" id="btn-upload" >选择上传文件</button>
            <span class="msg-span" style="color: #999;"></span>
            <!-- <input type="hidden" id="term-id" name="term_id" value="0"> -->
        </div>
        <div class="layui-row" style="margin-top:20px;">
            <button class="layui-btn layui-btn-disabled layui-btn-sm but-1" >下载失败文件</button>
            <button class="layui-btn layui-btn-danger layui-btn-sm but-2" style="display:none;margin-left:0px;" file-name="">下载失败文件</button>
        </div>
    </div>

@endsection
